package com.kullachi.jmx;

import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.GraphicsConfiguration;
import java.io.IOException;

import javax.management.InstanceNotFoundException;
import javax.management.MalformedObjectNameException;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
import javax.media.j3d.Background;
import javax.media.j3d.BoundingSphere;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.Transform3D;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.vecmath.Color3f;
import javax.vecmath.Point3d;
import javax.vecmath.Vector3d;

import com.kullachi.jmx.d3.MBean3D;
import com.kullachi.jmx.d3.MBeanServerConnectionAdapter;
import com.sun.j3d.utils.universe.SimpleUniverse;

public class Main extends Applet{
	public static SimpleUniverse u;

	
	@Override
	public void init() {
		//super.init();
		JFrame frame = new JFrame();
		//Applet frame= this;
		setLayout(new BorderLayout());
		GraphicsConfiguration config = SimpleUniverse
        .getPreferredConfiguration();
		
		Canvas3D c = new Canvas3D(config);
		frame.add("Center", c);
		
		/*JTree tree = new JTree();
	    JScrollPane scrollPane = new JScrollPane(tree);
	    frame.add("Top",scrollPane);*/

		//frame.setBounds(0, 0, 600, 600);
		
		u = new SimpleUniverse(c);
	    // This will move the ViewPlatform back a bit so the
	    // objects in the scene can be viewed.
	    u.getViewingPlatform().setNominalViewingTransform();
	    
	    Transform3D 	t 		= new Transform3D();
	    t.set(0.1,new Vector3d(0.2, 0.3, 0.0));
	    

	    MBean3D mbean;
		try {
			
		/*	Texture tex1 = new TextureLoader(getClass().getResource("/ecircle.jpg"),frame).getTexture();
			Texture tex2 = new TextureLoader(ImageIO.read(new URL("http://upload.wikimedia.org/wikipedia/commons/6/64/Bronze-jvan.jpg")),frame).getTexture();
			mbean = new MBean3D(true,false,t,tex1);*/
		
	    
		    BoundingSphere bounds = new BoundingSphere(new Point3d(0.2, 0.0, 0.0), 100.0);
	
	        // Set up the background
	        Color3f bgColor = new Color3f(0.05f, 0.05f, 0.2f);
	        Background bg = new Background(bgColor);
	        bg.setApplicationBounds(bounds);
	        BranchGroup arg0 = new BranchGroup();
	        arg0.addChild(bg);
			u.addBranchGraph(arg0 );
			
			/*MBeanServerConnectionAdapter adapter = new MBeanServerConnectionAdapter(mbsc,Main.u);
			adapter.adapt(new ObjectName("Ring:name=MailRingServer*"));*/
			
	       // mbean.addChild(bg);
	        
	        //u.addBranchGraph(mbean);
	        
	       /* t 		= new Transform3D();
		    t.set(0.1,new Vector3d(0.2, -0.3, 0.0));
	        u.addBranchGraph(new MBean3D(true,false,t,tex2));
	        
	        t 		= new Transform3D();
		    t.set(0.1,new Vector3d(-0.2, -0.3, 0.0));
	        u.addBranchGraph(new MBean3D(true,true,t,tex2));*/
	        
	        t 		= new Transform3D();
		    t.set(0.1,new Vector3d(-0.1, 0.3, 0.4));
	      //  u.addBranchGraph(new MBean3D(t));
		    frame.setBounds(0, 0, 600, 600);
	        frame.show();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	
	/**
	 * @param args
	 * @throws IOException 
	 * @throws NullPointerException 
	 * @throws MalformedObjectNameException 
	 * @throws InstanceNotFoundException 
	 */
	public static void main(String[] args) throws Exception{
		args = new String[1];
		args[0] = "service:jmx:rmi:///jndi/rmi://localhost:9999/jmxrmi";
		if(args.length < 1){
			System.out.println("Usage java -jar JMX3DConsole.jar <<URL>> <<enableGUI>>");
			System.exit(-1);
		}
		boolean isGuiEnabled = true;
		if(args.length>=2 && args[1] == "false"){
			isGuiEnabled	= false;
		}
		JMXServiceURL 			url 	=   new JMXServiceURL(args[0]);
		JMXConnector 			jmxc 	= 	JMXConnectorFactory.connect(url, null);
		if(isGuiEnabled){
			Main	gui = new Main();
			gui.init();
			MBeanServerConnectionAdapter adapter = new MBeanServerConnectionAdapter(jmxc,Main.u);
			adapter.adapt("Ring:name=MailRingServer");
			adapter.adapt("Ring:name=Tripoli");
			adapter.adapt("Ring:name=Ex");
			adapter.adapt("Ring:name=Co");
			adapter.adapt("Ring:name=Ec");
		}
	}

}
